#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int id;
    int score;
    struct Node *next;
} Node;

Node* createNode(int id, int score) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->id = id;
    newNode->score = score;
    newNode->next = NULL;
    return newNode;
}

void insertNode(Node **head, int id, int score) {
    Node* newNode = createNode(id, score);
    if (*head == NULL) {
        *head = newNode;
    } else {
        Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

Node* mergeLists(Node *head1, Node *head2) {
    Node *dummyHead = createNode(0, 0);
    Node *tail = dummyHead;

    while (head1 != NULL && head2 != NULL) {
        if (head1->id < head2->id) {
            tail->next = head1;
            head1 = head1->next;
        } else {
            tail->next = head2;
            head2 = head2->next;
        }
        tail = tail->next;
    }

    if (head1 != NULL) {
        tail->next = head1;
    } else if (head2 != NULL) {
        tail->next = head2;
    }

    return dummyHead->next;
}

void printList(Node *head) {
    while (head != NULL) {
        printf("%d %d\n", head->id, head->score);
        head = head->next;
    }
}

int main() {
    int N, M, id, score;
    scanf("%d %d", &N, &M);

    Node *head1 = NULL, *head2 = NULL;

    for (int i = 0; i < N; i++) {
        scanf("%d %d", &id, &score);
        insertNode(&head1, id, score);
    }

    for (int i = 0; i < M; i++) {
        scanf("%d %d", &id, &score);
        insertNode(&head2, id, score);
    }

    Node *mergedHead = mergeLists(head1, head2);
    printList(mergedHead);

    Node *temp;
    while (mergedHead != NULL) {
        temp = mergedHead;
        mergedHead = mergedHead->next;
        free(temp);
    }

    return 0;
}